﻿using UnityEngine;

public class RtsExpeditionFormation : MonoBehaviour
{
    public void Log()
    {
        var str = "阵型"+gameObject.name+":\n";
        var self = transform.Find("self");
        if (self)
        {
            str += "\t";
            str += "我方:\n";
            for (int i = 1; i <= 5; i++)
            {
                var child = self.Find(i.ToString());
                if (child)
                {
                    str += "\t\t";
                    str += string.Format("{0} : x = {1:F2} ; z = {2:F2} ; rotation = {3:F2} \n", i, child.position.x, child.position.z, GetServerAngle(child.eulerAngles.y));
                }
            }
        }
        var enemy = transform.Find("enemy");
        if (enemy)
        {
            str += "\t";
            str += "敌方:\n";
            for (int i = 1; i <= 5; i++)
            {
                var child = enemy.Find(i.ToString());
                if (child)
                {
                    str += "\t\t";
                    str += string.Format("{0} : x = {1:F2} ; z = {2:F2} ; rotation = {3:F2} \n", i, child.position.x, child.position.z, GetServerAngle(child.eulerAngles.y));
                }
            }
        }
        Debug.LogError(str);
    }

    private float GetServerAngle(float clientAngle)
    {
        var angle = -(clientAngle - 90);
        if (angle > 360)
        {
            angle = angle - 360;
        }
        else if (angle < 0)
        {
            angle = angle + 360;
        }

        return angle;
    }
}